import Vue from 'vue';
import store from '@/store';
export const presOpen = true;

// jsx
// const pDelete = [{ name: 'permission', value: 'user/delete' }];
// const pUpdate = [{ name: 'permission', value: 'user/update' }];
//  <a-button {...{ directives: pDelete }}
//  <a-button {...{ directives: pUpdate }}

// 权限指令 一般用指令，特殊情况用判断方法 比如vif判断
Vue.directive('permission', {
  inserted(el, binding) {
    const permission = binding.value;
    if (permission && presOpen) {
      const hasPermission = store.getters['user/listRoleResource'].includes(permission);
      if (!hasPermission) {
        // 没有权限 移除Dom元素
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      //
    }
  }
});
// 权限判断方法
export const permissionHandler = (permission) => {
  const hasPermission = store.getters['user/listRoleResource'].includes(permission);
  if (!hasPermission && presOpen) {
    // 没有权限 移除Dom元素
    return false;
  } else {
    return true;
  }
};

Vue.prototype.permissionHandler = permissionHandler;

// menu
export const pres = (menuList, mr) => {
  const menuRoles = mr ?? store.getters['user/menuList'] ?? [];
  let ml = menuList;
  if (presOpen) {
    ml = menuList.filter(item => {
      return menuRoles.findIndex(item2 => item2.title === item.meta.role) > -1 || item.meta.role === 'allow';
    });
  }

  return ml;
};
